package com.service.ua;

import com.bpms.common.Base64;
import com.bpms.common.CommonUtil;
import com.bpms.common.Constant;
import com.bpms.common.CreateID;
import com.bpms.common.Db;
import com.bpms.common.EnDES;
import com.bpms.common.Tools;
import com.bpms.common.VO;
import com.bpms.common.VOList;
import com.bpms.esb.IService;
import com.zk.atm.utils.ConstantsAtmp;

public class Login implements IService {
	
	/**
	 * 输入参数
	 * userid	用户id
	 * remoteip  远程IP
	 * passwd   密码
	 * sysid   系统登录权限
	 * 输出参数
	 * perms  权限
	 * result 0000成功 1111失败 9999其他
	 * msg 描述
	 * user信息
	 * SYSID
	 */
	public VO exec(VO args) {
		
		VO info=new VO();
		if(args==null){
			info.setAttr("result", "9999");
			info.setAttr("msg", "参数为空");
			return info;
		}
		try {
			Db j=new Db();
			StringBuffer userSql=new StringBuffer();
			userSql.append("select a.*,b.orgtype from BPM_USER a ");
			userSql.append("left join bpm_org b on a.orgid=b.autoid");
			userSql.append(" where a.userid='");
			userSql.append(args.getAttr("USERID"));
			userSql.append("'");
			VO uservo=j.readInCombination(new VO(), userSql.toString());
			String remoteip = args.getAttr("remoteip");
			if(uservo==null)
			{
				info.setAttr("result", "9999");
				info.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "没有该用户，请从新输入!");
				return info;
			}
			
			if(uservo.getAttr("DISABLE").equals("Y"))
			{
				info.setAttr("result", "9999");
				info.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "用户已经停用!");
				return info;
			}
			
//			if("login".equalsIgnoreCase(uservo.getAttr("LOGSTATUS"))&&!"admin".equalsIgnoreCase(args.getAttr("USERID")))
//			{
//				info.setAttr("result", "9999");
//				info.setAttr("msg", "该用户已经登录");
//				return info;
//			}
			
//			String enpwd=new EnDES().encrypt(args.getAttr("passwd"));
//			String desPwd=Base64.encodeBytes(enpwd.getBytes());
			if(!args.getAttr("passwd").equals(uservo.getAttr("passwd")))
			{
				info.setAttr("result", "9999");
				info.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "用户密码错误!");
				return info;
			}
			
			
			
//			StringBuffer permsql=new StringBuffer();
//			permsql.append("select b.* from bpm_role a ");
//			permsql.append("left join BPM_ROLEPERM b on a.roleid=b.roleid ");
//			permsql.append("where a.ROLEID='");
//			permsql.append(uservo.getAttr("ROLEID"));
//			permsql.append("' and b.PERMID='");
//			permsql.append(args.getAttr("SYSID"));
//			permsql.append("'");
//			VO permvo=j.readInCombination(new VO(), permsql.toString());
//			if(permvo==null)
//				{
//				info.setAttr("result", "false");
//				info.setAttr("msg", "the user perm is not enough");
//				return info;
//			}
			
//			StringBuffer sql=new StringBuffer();
//			sql.append("select  decode(val,'01',tasktypeid,'02',a.permid) as permid,r.roleid,r.groupid ");
//			sql.append(" from BPM_ROLEPERM a left join bpm_perm b on a.permid=b.permid left join bpm_role r on a.roleid=r.roleid ");
//			sql.append(" where a.roleid in (select roleid from bpm_userrole where userid='"+args.getAttr("USERID")+"')");
//			VOList perms=j.readAll(new VO(), sql.toString());
//			String s_perms="";
//			String groupid="";
//			for(int i=0;i<perms.listData.size();i++){
//				VO p=(VO)perms.listData.get(i);
//				s_perms=s_perms+p.getAttr("PERMID")+",";
//				if(p.getAttr("permid").equalsIgnoreCase(args.getAttr("SYSID"))){
//					groupid=p.getAttr("groupid")==null?"":p.getAttr("groupid");
//				}
//				
//			}
//			
//			if("".equals(s_perms)){
//				info.setAttr("result", "9999");
//				info.setAttr("msg", "1006");
//				return info;
//			}
			
			VO keyVO=new VO();
			VO updateVO=new VO();
			keyVO.voType="bpm_user";
			keyVO.isAutoKey=false;
			keyVO.keyString="userid";
			keyVO.setAttr("userid",args.getAttr("USERID"));
			updateVO.setAttr("lastlogindate",Tools.getDate());
			updateVO.setAttr("lastlogintime",Tools.getTime());
			updateVO.setAttr("loginip",remoteip);
			updateVO.setAttr("logstatus","login");
			j.update(updateVO, keyVO);
			
			VO v=new VO();
			v.voType="UA_USERLOG";
			String ID=CreateID.I().CreateCheckID("UA_USERLOG");
			v.setAttr("ID", ID);
			v.setAttr("USERID", uservo.getAttr("USERID"));
			v.setAttr("SYSID", args.getAttr("SYSID"));
			v.setAttr("USERACTION", "LOGIN");
			v.setAttr("CDATE", Tools.getDate());
			v.setAttr("CTIME", Tools.getTime());
			j.insert(v);
			uservo.setAttr("SYSID", args.getAttr("SYSID"));
//			uservo.setAttr("PERMS", s_perms);
			uservo.setAttr("result", "0000");
//			uservo.setAttr("groupid", groupid);
			VOList volist=new VOList();
			volist.listData.add(uservo);
			info.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_RET, "0");
			info.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "登录成功!");
			info.setObj(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_VOLIST, volist);
			
			return info;
			
		} catch (Exception e) {
			CommonUtil.debug("ua.login exception",e);
			//e.printStackTrace();
			info.setAttr("result", "9999");
			info.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "系统异常");
			return info;
		}
		
	
	}

}
